package com.quartz.example;


import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

import static org.quartz.JobBuilder.newJob;

public class QuartzTest implements Job  {
    Scheduler scheduler;
    String jobName;
    int count;

    public QuartzTest(String jobName) throws Exception {
        this.count = 1 ;
        this.jobName = jobName;
        scheduler = StdSchedulerFactory.getDefaultScheduler();
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        if(count>=5){
            System.out.println(String.format("今天晚上:%s,%s次，明天再来",jobName,count));
            try {
                scheduler.shutdown();
            } catch (SchedulerException e) {
                e.printStackTrace();
            }

        }
        System.out.println(String.format("执行任务:%s,开始时间:%s,操了%s次",jobName,new Date(),count));
        count++;
    }

    public static void main(String[] args) throws  Exception {

        QuartzTest quartzTest =  new QuartzTest("吃包子");
        quartzTest.scheduler.start();

    }
}